package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Coal;
import com.tianxin.workbao.workBao.holder.EmptyHolder;

import java.util.List;

/**
 * Created by hacker on 2017/7/14.
 */

public class CoalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<Coal> mCoals;
    private Context mContext;

    public CoalAdapter(List<Coal> coals, Context context) {
        this.mCoals = coals;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mCoals.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mCoals.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (mCoals.size() > 0) {
            return mCoals.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView coalNameTV;
        TextView coalStorageTV;

        public ViewHolder(View itemView) {
            super(itemView);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            coalStorageTV = (TextView) itemView.findViewById(R.id.coal_storage);
        }

        public void BindViewData(Coal coal) {
            coalNameTV.setText(coal.getTypename());
            coalStorageTV.setText(String.valueOf(coal.getStorage())+"吨");
        }
    }
}
